<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>集合length属性常用于遍历集合中的元素</title>
</head>

<body>
    <h2>JavaScript HTML DOM</h2>
    <p>Hello World!</p>
    <p>Hello Runoob!</p>
    <p>点击按钮修改 p 元素的背景颜色。</p>
    <button onclick="myFunction()">点我</button>
    <script>
        function myFunction() {
            var myCollection = document.getElementsByTagName("p");
            var i;
            for (i = 0; i < myCollection.length; i++) {
                myCollection[i].style.color = "red";
            }
        }
    </script>
</body>

</html>